package hourglassSum;

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class HourglassSum {

    // Complete the hourglassSum function below.
    static int hourglassSum(int[][] arr) {
        int max = Integer.MIN_VALUE;
        int[] maxIndex = {1, 1};

        if (arr == null) {
            throw new NullPointerException("Array cannot be null.");
        }

        for (int i = 1; i < arr.length - 1; i++) { // row
            for (int j = 1; j < arr[i].length - 1; j++) { // column
                int innerSum = innerSum(arr, i, j);
                if (max < innerSum) {
                    max = innerSum;
                    maxIndex[0] = i;
                    maxIndex[1] = j;
                }
            }

        }
        return max;
    }

    static int innerSum(int[][] arr, int row, int column) {
        int element1 = arr[row - 1][column - 1];
        int element2 = arr[row - 1][column];
        int element3 = arr[row - 1][column + 1];
        int center = arr[row][column];
        int element4 = arr[row + 1][column - 1];
        int element5 = arr[row + 1][column];
        int element6 = arr[row + 1][column + 1];

        return element1 + element2 + element3 + center + element4 + element5 + element6;
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("C:\\Users\\Kiba\\Desktop\\hourglass.txt"));

        int[][] arr = {
                {-1,-1, 0,-9,-2,-2},
                {-2,-1,-6,-8,-2,-5},
                {-1,-1,-1,-2,-3,-4},
                {-1,-9,-2,-4,-4,-5},
                {-7,-3,-3,-2,-9,-9},
                {-1,-3,-1,-2,-4,-5}};
//        int[][] arr = new int[6][6];
//
//        for (int i = 0; i < 6; i++) {
//            String[] arrRowItems = scanner.nextLine().split(" ");
//            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
//
//            for (int j = 0; j < 6; j++) {
//                int arrItem = Integer.parseInt(arrRowItems[j]);
//                arr[i][j] = arrItem;
//            }
//        }

        int result = hourglassSum(arr);

        System.out.println("result = " + result);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}
